package com.jinlejia.erp.ui.customer;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ActivityPushPopTelInfoBinding;
import com.jinlejia.erp.utils.CallUtils;
import com.weight.CustomToast;


/**
 * 推送弹出客户联系信息
 */
public class PushPopTelInfoActivity extends AppCompatActivity {

    ClipboardManager cmb;

    private ActivityPushPopTelInfoBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_push_pop_tel_info);
        binding.setEvent(this);
        binding.mobileNumber.setOnLongClickListener(this::onViewLongClicked);
        binding.homePhone.setOnLongClickListener(this::onViewLongClicked);
        binding.officePhone.setOnLongClickListener(this::onViewLongClicked);
        binding.linkerTelNumber.setOnLongClickListener(this::onViewLongClicked);

        cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        JSONObject data = JSON.parseObject(getIntent().getStringExtra("content"));
        setData(data);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        JSONObject data = JSON.parseObject(intent.getStringExtra("content"));
        setData(data);
    }

    private void setData(JSONObject data) {
        binding.owerName.setText(data.getString("cusName"));
        binding.linkerName.setText(data.getString("contactsName"));
        if (data.containsKey("contactsTel") && !TextUtils.isEmpty(data.getString("contactsTel"))) {
            binding.linkerTelNumber.setVisibility(View.VISIBLE);
            binding.linkerTelNumber.setText(data.getString("contactsTel"));
        } else {
            binding.linkerTelNumber.setVisibility(View.GONE);
        }
        JSONObject object = data.getJSONObject("mapTel");

        if (object.containsKey("手机")) {
            binding.layout2.setVisibility(View.VISIBLE);
            binding.mobileNumber.setText(object.getString("手机"));
        } else {
            binding.layout2.setVisibility(View.GONE);
        }
        if (object.containsKey("住宅")) {
            binding.layout3.setVisibility(View.VISIBLE);
            binding.homePhone.setText(object.getString("住宅"));
        } else {
            binding.layout3.setVisibility(View.GONE);
        }
        if (object.containsKey("办公")) {
            binding.layout6.setVisibility(View.VISIBLE);
            binding.officePhone.setText(object.getString("办公"));
        } else {
            binding.layout6.setVisibility(View.GONE);
        }

    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close:
                finish();
                break;
            case R.id.mobile_number:
                CallUtils.CallTel(PushPopTelInfoActivity.this, binding.mobileNumber.getText().toString());
                break;
            case R.id.home_phone:
                CallUtils.CallTel(PushPopTelInfoActivity.this, binding.homePhone.getText().toString());
                break;
            case R.id.office_phone:
                CallUtils.CallTel(PushPopTelInfoActivity.this, binding.officePhone.getText().toString());
                break;
            case R.id.linker_tel_number:
                CallUtils.CallTel(PushPopTelInfoActivity.this, binding.linkerTelNumber.getText().toString());
                break;
            case R.id.call_btn:
                String tel = "";
                if (!TextUtils.isEmpty(binding.mobileNumber.getText())) {
                    tel = binding.mobileNumber.getText().toString();
                } else if (!TextUtils.isEmpty(binding.homePhone.getText())) {
                    tel = binding.homePhone.getText().toString();
                } else if (!TextUtils.isEmpty(binding.officePhone.getText())) {
                    tel = binding.officePhone.getText().toString();
                }
                CallUtils.CallTel(PushPopTelInfoActivity.this, tel);
                break;
            default:
                break;
        }
    }


    public boolean onViewLongClicked(View view) {
        switch (view.getId()) {
            case R.id.mobile_number:
                ClipData clip = ClipData.newPlainText("tel", binding.mobileNumber.getText());
                cmb.setPrimaryClip(clip);
                CustomToast.showToast(PushPopTelInfoActivity.this, "已复制号码，请在拨号页面粘贴拨打", 1500);
                break;
            case R.id.home_phone:
                ClipData clip1 = ClipData.newPlainText("tel", binding.homePhone.getText());
                cmb.setPrimaryClip(clip1);
                CustomToast.showToast(PushPopTelInfoActivity.this, "已复制号码，请在拨号页面粘贴拨打", 1500);
                break;
            case R.id.office_phone:
                ClipData clip2 = ClipData.newPlainText("tel", binding.officePhone.getText());
                cmb.setPrimaryClip(clip2);
                CustomToast.showToast(PushPopTelInfoActivity.this, "已复制号码，请在拨号页面粘贴拨打", 1500);
            case R.id.linker_tel_number:
                ClipData clip3 = ClipData.newPlainText("tel", binding.linkerTelNumber.getText());
                cmb.setPrimaryClip(clip3);
                CustomToast.showToast(PushPopTelInfoActivity.this, "已复制号码，请在拨号页面粘贴拨打", 1500);
                break;
            default:
                break;
        }

        return true;
    }

}
